<?php

namespace app\adminapi\controller\v1\system;

use app\adminapi\controller\AuthController;
use app\services\other\CarouselServices;
use think\facade\App;

class Carousel extends AuthController
{
    /**
     * user constructor.
     * @param App $app
     * @param CarouselServices $services
     */
    public function __construct(App $app, CarouselServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    public function index()
    {
        [$list, $count] = $this->services->list();
        return app('json')->success(compact('list', 'count'));
    }

    public function add()
    {
        $data = $this->request->postMore([
            ['image', ''],
            ['status', 1],
        ]);
        $data['add_time'] = time();
        if ($this->services->save($data)) {
            return app('json')->success('操作成功');
        }
        return app('json')->success('操作失败');
    }

    public function edit($id)
    {
        $data = $this->request->postMore([
            ['image', ''],
            ['status', 1],
        ]);
        if (empty($data['image'])) {
            unset($data['image']);
        }
        if ($this->services->update(['id' => (int)$id], $data)) {
            return app('json')->success('操作成功');
        }
        return app('json')->success('操作失败');
    }


}